﻿package com.eitb.programas.olentzero09.objetos{
	import flash.events.*;
	import flash.display.*;
	
	import com.eitb.util.*;
	import com.eitb.programas.olentzero09.*;
	import com.eitb.programas.olentzero09.events.*;
	
	public class OvniVacio extends MovieClip {
		private static const ANGULO_INICIAL:Number=-1;
		private static const ANGULO_FINAL:Number=1;
		private static const ANGULO_INCREMENTO:Number=.05;
		//private static const gravedad:Number=0.01;
		//private var velocidad:Number;
		//private var rotacion:Number;
		//private var viento:Number;
		private var angulo:Number;
		private var subir:Boolean;
		private var limites:Object;
		

		public function OvniVacio():void {
			this.angulo = OvniVacio.ANGULO_INICIAL;
			this.subir = true;
			this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
		}
		
		public function initLimites(limites:Object):void {
			this.limites = limites;
		}
		
		public function init():void {
			this.x = 0;
			this.y = 88 + Math.random()*37;
		}
		
		public function onEnterFrame(evt:Event):void {
			if (this.angulo > OvniVacio.ANGULO_FINAL) this.subir = false;
			if (this.angulo < OvniVacio.ANGULO_INICIAL) this.subir = true;
			
			if (this.subir) {
				this.angulo+=OvniVacio.ANGULO_INCREMENTO;
			} else {
				this.angulo-=OvniVacio.ANGULO_INCREMENTO;
			}
			
			trace(this.angulo);
			this.x += Math.cos(this.angulo);
			this.y -= Math.sin(this.angulo);
			
			if (this.isOut()) {
				this.finalize();
			}
		}
		
		
		private function isOut():Boolean {
			return (this.x < this.limites.x) || (this.x > this.limites.xMax);
		}
		
		public function finalize():void {
			try {
				this.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
				this.parent.removeChild(this);
			} catch (e:Error) {
				trace(e);
			}
		}
		
		public function colision():void {
			this.parent.dispatchEvent(new OlentzeroEvent(OlentzeroEvent.COLISION, {colision:"ovni",x:this.x,y:this.y}));
			this.finalize();
		}
	}
}